//
//  XPayView.swift
//  d11
//
//  Created by Jar Su on 1/24/20.
//  Copyright © 2020 Tech_001. All rights reserved.
//

import Foundation
class XPayView : UIView{
    var money2 : UITextField = UITextField.init()
    var money3 : UITextField = UITextField.init()
    var delegate:Pay?
    var data : [String:Any] = [:]
    convenience init(data:[String:Any]) {
        self.init(frame: UIScreen.main.bounds)
        self.data = data
        self.initUi(data: data)
    }
     
    
    func initUi(data:[String:Any]){
                self.backgroundColor = RGBAlpa(1,1,1,0.4)
                self.tag = 1111
                let window: UIWindow  = ((UIApplication.shared.delegate?.window)!)!
                let effecView = UIHelper.showBlurEffect(isClick:true)
                window.addSubview(effecView)
                window.addSubview(self)
                self.snp.makeConstraints { (make) in
                    make.width.equalToSuperview()
                    make.height.equalToSuperview()
                }
                let whiteView :UIView  = UIView.init(frame: UIScreen.main.bounds)
                       whiteView.backgroundColor = UIColor.init(white: 1, alpha: 1)
                       whiteView.layer.cornerRadius = 5;
                       whiteView.layer.masksToBounds = true
                       self.addSubview(whiteView)
                       whiteView.snp.makeConstraints { (make) in
                           make.left.equalToSuperview().offset(10)
                           make.right.equalToSuperview().offset(-10)
                           make.top.equalToSuperview().offset(80)
                           make.centerX.equalToSuperview()
                           make.height.equalTo(320)
                       }
                       
                       let  titleBtn : UIButton  = UIButton.init(type: .custom)
                       titleBtn.setBackgroundImage(imageNamed("icon_common_pop_window_title_bg"), for: .normal)
                       titleBtn.setTitle("快捷银行卡支付", for: .normal)
                       titleBtn.adjustsImageWhenHighlighted = false
                       titleBtn.titleEdgeInsets = UIEdgeInsets(top: -10,left: 0, bottom: 0, right: 0)
                       whiteView.addSubview(titleBtn)
                       titleBtn.snp.makeConstraints { (make) in
                           make.width.equalToSuperview()
                           make.height.equalTo(59)
                           make.centerX.equalToSuperview()
                        make.top.equalToSuperview()
                       }
                let closeImg = UIImageView.init()
                closeImg.image = UIImage.init(named: "icon_common_pop_window_close")
                closeImg.isUserInteractionEnabled = true
                let tap1 = UITapGestureRecognizer.init(target: self, action: #selector(actionToCancel))
                closeImg.addGestureRecognizer(tap1)
                whiteView.addSubview(closeImg)
                closeImg.snp.makeConstraints { (make) in
                    make.width.equalTo(20)
                    make.height.equalTo(20)
                    make.right.equalToSuperview().offset(-10)
                    make.top.equalToSuperview().offset(10)
                }
                
                let moneyView = UIView.init()
                whiteView.addSubview(moneyView)
                moneyView.snp.makeConstraints { (make) in
                    make.left.equalToSuperview()
                    make.right.equalToSuperview()
                    make.height.equalTo(50)
                    make.top.equalTo(titleBtn.snp_bottom).offset(10)
                }
                let moneyImg = UIImageView.init()
                moneyImg.image = UIImage.init(named: "icon_depoist_input_info_money")
                moneyView.addSubview(moneyImg)
                moneyImg.snp.makeConstraints { (make) in
                    make.width.equalTo(25)
                    make.height.equalTo(25)
                    make.centerY.equalToSuperview()
                    make.left.equalTo(10)
                }
                let starView1 = UILabel.init()
                starView1.text = "*"
                starView1.font = UIFont.systemFont(ofSize: 15)
                starView1.textColor = UIColor.red
                moneyView.addSubview(starView1)
                starView1.snp.makeConstraints { (make) in
        //            make.width.equalTo(9)
        //            make.height.equalTo(9)
                    make.centerY.equalToSuperview()
                    make.left.equalTo(moneyImg.snp_right).offset(10)
                }
                let moneyLabel = UILabel.init()
                moneyLabel.text = "充值金额:"
                moneyLabel.font = UIFont.systemFont(ofSize: 15)
                moneyLabel.textColor = UIColor.black
                moneyView.addSubview(moneyLabel)
                moneyLabel.snp.makeConstraints { (make) in
                    make.width.equalTo(100)
        //            make.height.equalTo(9)
                    make.left.equalTo(starView1.snp_right)
                    make.centerY.equalToSuperview()
                }
        let formparam = data["formparam"] as? [String:String] ?? [:]
                let money = UILabel.init()
        money.text = formparam["expressCardAmount"] ?? ""
                money.font = UIFont.systemFont(ofSize: 15)
                money.textColor = UIColor.black
                moneyView.addSubview(money)
                money.snp.makeConstraints { (make) in
                    make.width.equalToSuperview()
        //            make.height.equalTo(10)
                    make.left.equalTo(moneyLabel.snp_right)
                    make.centerY.equalToSuperview()
                }
                let line1 = UIView()
                line1.backgroundColor = UIColor.init("#dddddd")
                whiteView.addSubview(line1)
                line1.snp.makeConstraints{ (make) in
                    make.height.equalTo(1)
                    make.width.equalToSuperview()
                    make.top.equalTo(moneyView.snp_bottom)
                }
                
                let moneyView2 = UIView.init()
                        whiteView.addSubview(moneyView2)
                        moneyView2.snp.makeConstraints { (make) in
                            make.left.equalToSuperview()
                            make.right.equalToSuperview()
                            make.height.equalTo(50)
                            make.top.equalTo(line1.snp_bottom)
                        }
                        let moneyImg2 = UIImageView.init()
                        moneyImg2.image = UIImage.init(named: "icon_depoist_input_info_name")
                        moneyView2.addSubview(moneyImg2)
                        moneyImg2.snp.makeConstraints { (make) in
                            make.width.equalTo(25)
                            make.height.equalTo(25)
                            make.centerY.equalToSuperview()
                            make.left.equalTo(10)
                        }
                        let starView2 = UILabel.init()
                        starView2.text = "*"
                        starView2.font = UIFont.systemFont(ofSize: 18)
                        starView2.textColor = UIColor.red
                        moneyView2.addSubview(starView2)
                        starView2.snp.makeConstraints { (make) in
                //            make.width.equalTo(9)
                //            make.height.equalTo(9)
                            make.centerY.equalToSuperview()
                            make.left.equalTo(moneyImg2.snp_right).offset(10)
                        }
                        let moneyLabel2 = UILabel.init()
                        moneyLabel2.text = "银行卡户名:"
                        moneyLabel2.font = UIFont.systemFont(ofSize: 15)
                        moneyLabel2.textColor = UIColor.black
                        moneyView2.addSubview(moneyLabel2)
                        moneyLabel2.snp.makeConstraints { (make) in
                            make.width.equalTo(100)
                //            make.height.equalTo(9)
                            make.left.equalTo(starView2.snp_right)
                            make.centerY.equalToSuperview()
                        }
                        money2 = UITextField.init()
                        money2.placeholder = "银行卡户名"
                        money2.textAlignment = NSTextAlignment.left
                        money2.font = UIFont.systemFont(ofSize: 15)
                        money2.textColor = UIColor.black
                        moneyView2.addSubview(money2)
                        money2.snp.makeConstraints { (make) in
                            make.width.equalToSuperview()
                //            make.height.equalTo(10)
                            make.left.equalTo(moneyLabel2.snp_right)
                            make.centerY.equalToSuperview()
                        }
                        let line2 = UIView()
                        line2.backgroundColor = UIColor.init("#dddddd")
                        whiteView.addSubview(line2)
                        line2.snp.makeConstraints{ (make) in
                            make.height.equalTo(1)
                            make.width.equalToSuperview()
                            make.top.equalTo(moneyView2.snp_bottom)
                        }
                
                let moneyView3 = UIView.init()
                        whiteView.addSubview(moneyView3)
                        moneyView3.snp.makeConstraints { (make) in
                            make.left.equalToSuperview()
                            make.right.equalToSuperview()
                            make.height.equalTo(50)
                            make.top.equalTo(line2.snp_bottom)
                        }
                        let moneyImg3 = UIImageView.init()
                        moneyImg3.image = UIImage.init(named: "icon_depoist_input_info_bankNo")
                        moneyView3.addSubview(moneyImg3)
                        moneyImg3.snp.makeConstraints { (make) in
                            make.width.equalTo(25)
                            make.height.equalTo(25)
                            make.centerY.equalToSuperview()
                            make.left.equalTo(10)
                        }
                        let starView3 = UILabel.init()
                        starView3.text = "*"
                        starView3.font = UIFont.systemFont(ofSize: 15)
                        starView3.textColor = UIColor.red
                        moneyView3.addSubview(starView3)
                        starView3.snp.makeConstraints { (make) in
                //            make.width.equalTo(9)
                //            make.height.equalTo(9)
                            make.centerY.equalToSuperview()
                            make.left.equalTo(moneyImg3.snp_right).offset(10)
                        }
                        let moneyLabel3 = UILabel.init()
                        moneyLabel3.text = "银行卡号:"
                        moneyLabel3.font = UIFont.systemFont(ofSize: 15)
                        moneyLabel3.textColor = UIColor.black
                        moneyView3.addSubview(moneyLabel3)
                        moneyLabel3.snp.makeConstraints { (make) in
                            make.width.equalTo(100)
                //            make.height.equalTo(9)
                            make.left.equalTo(starView3.snp_right)
                            make.centerY.equalToSuperview()
                        }
                        money3 = UITextField.init()
                        money3.placeholder = "银行卡号"
                        money3.textAlignment = NSTextAlignment.left
                        money3.font = UIFont.systemFont(ofSize: 15)
                        money3.keyboardType = .numberPad
                        money3.textColor = UIColor.black
                        moneyView3.addSubview(money3)
                        money3.snp.makeConstraints { (make) in
                            make.width.equalToSuperview()
                //            make.height.equalTo(10)
                            make.left.equalTo(moneyLabel3.snp_right)
                            make.centerY.equalToSuperview()
                        }
                        let line3 = UIView()
                        line3.backgroundColor = UIColor.init("#dddddd")
                        whiteView.addSubview(line3)
                        line3.snp.makeConstraints{ (make) in
                            make.height.equalTo(1)
                            make.width.equalToSuperview()
                            make.top.equalTo(moneyView3.snp_bottom)
                        }
                
                let btn = UIButton.init()
                btn.setTitle("提交", for: .normal)
                btn.addTarget(self, action: #selector(self.xpay), for: .touchUpInside)
                btn.setBackgroundImage(UIImage.init(named: "icon_depoist_checking_btn_bg"), for: .normal)
                whiteView.addSubview(btn)
                btn.snp.makeConstraints{ (make) in
                    make.left.equalToSuperview().offset(10)
                    make.right.equalToSuperview().offset(-10)
                    make.height.equalTo(50)
                    make.top.equalTo(line3.snp_bottom).offset(20)
                }
    }
    
    @objc func actionToCancel(){
        UIHelper.removeEffect()
        removeFromSuperview()
//             PopWindowHelper.dissmissPopView(tag: 1111)
       }
    
    @objc func xpay(){
        if(money2.text!.isEmpty){
            UIHelper.alertSimpleMessage(about: "请输入银行卡户名", withDescription: "")
            return
        }
        if(money3.text!.isEmpty){
            UIHelper.alertSimpleMessage(about: "请输入银行卡号", withDescription: "")
            return
        }
        delegate?.pay(data: data , name: money2.text ?? "", card: money3.text ?? "")
        actionToCancel()
    }
}
